/**
 * Copyright (c) 2011-2017, dafei 李飞 (myaniu AT gmail DOT com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package top.jnaw.jee.plugin.shiro;

import java.util.concurrent.ConcurrentMap;
import javax.sql.DataSource;


/**
 * ShiroKit. (Singleton, ThreadSafe)
 *
 * @author dafei
 */
public class ShiroKit {

  /**
   * 登录成功时所用的页面。
   */
  private static String successUrl = "/";

  /**
   * 登录成功时所用的页面。
   */
  private static String loginUrl = "/login.html";

  /**
   * action的扩展名，比如.action或者 .do
   */
  private static String extName = "";

  /**
   * 登录成功时所用的页面。
   */
  private static String unauthorizedUrl = "/401.jsp";

  private static DataSource dataSource = null;

  private static boolean useSalt = true;
  private static int hashIterations = 1;
  private static String hashAlgorithmName = "MD5";

  /**
   * Session中保存的请求的Key值
   */
  private static String SAVED_REQUEST_KEY = "jfinalShiroSavedRequest";


  /**
   * 用来记录那个action或者actionpath中是否有shiro认证注解。
   */
  private static ConcurrentMap<String, top.jnaw.jee.plugin.shiro.AuthzHandler> authzMaps = null;

  /**
   * 禁止初始化
   */
  private ShiroKit() {
  }

  static void init(
      ConcurrentMap<String, top.jnaw.jee.plugin.shiro.AuthzHandler> maps) {
    authzMaps = maps;
  }

  static top.jnaw.jee.plugin.shiro.AuthzHandler getAuthzHandler(
      String actionKey) {
    /*
    if(authzMaps.containsKey(controllerClassName)){
			return true;
		}*/
    return authzMaps.get(actionKey);
  }

  public static final String getSuccessUrl() {
    return successUrl;
  }

  public static final void setSuccessUrl(String successUrl) {
    top.jnaw.jee.plugin.shiro.ShiroKit.successUrl = successUrl;
  }

  public static final String getLoginUrl() {
    return loginUrl;
  }


  public static final void setLoginUrl(String loginUrl) {
    top.jnaw.jee.plugin.shiro.ShiroKit.loginUrl = loginUrl;
  }

  public static final String getUnauthorizedUrl() {
    return unauthorizedUrl;
  }

  public static final void setUnauthorizedUrl(String unauthorizedUrl) {
    top.jnaw.jee.plugin.shiro.ShiroKit.unauthorizedUrl = unauthorizedUrl;
  }

  public static DataSource getDataSource() {
    return dataSource;
  }

  public static void setDataSource(DataSource dataSource) {
    top.jnaw.jee.plugin.shiro.ShiroKit.dataSource = dataSource;
  }

  public static boolean isUseSalt() {
    return useSalt;
  }

  public static void setUseSalt(boolean useSalt) {
    top.jnaw.jee.plugin.shiro.ShiroKit.useSalt = useSalt;
  }

  public static int getHashIterations() {
    return hashIterations;
  }

  public static void setHashIterations(int hashIterations) {
    top.jnaw.jee.plugin.shiro.ShiroKit.hashIterations = hashIterations;
  }

  public static String getHashAlgorithmName() {
    return hashAlgorithmName;
  }

  public static void setHashAlgorithmName(String hashAlgorithmName) {
    top.jnaw.jee.plugin.shiro.ShiroKit.hashAlgorithmName = hashAlgorithmName;
  }

  /**
   * Session中保存的请求的Key值
   */
  public static final String getSavedRequestKey() {
    return SAVED_REQUEST_KEY;
  }

  public static final String getExtName() {
    return extName;
  }

  /**
   * 扩展名，比如.action／.do
   */
  public static final void setExtName(String extName) {
    top.jnaw.jee.plugin.shiro.ShiroKit.extName = extName;
  }
}
